Inside Macintosh: Files

Previous | Chapter Top | Chapter Contents | Next

Adjusting the File Menu

Your application should dim any File menu commands that are not available at the time the user pulls down the File menu. For example, if your application does not yet have a document window open, then the Save, Save As, and Revert commands should be dimmed. You can adjust the File menu easily using the technique shown in Listing 1-19 .

Listing 19 Adjusting the File menu

PROCEDURE DoAdjustFileMenu;
VAR
    myWindow:       WindowPtr;
    myMenu:         MenuHandle;
    myData:         MyDocRecHnd;                        {handle to window data}

BEGIN
    myWindow := FrontWindow;
    IF myWindow = NIL THEN
        BEGIN
            myMenu := GetMHandle(mFile);
            DisableItem(myMenu, iSave);                 {disable Save}
            DisableItem(myMenu, iSaveAs);               {disable Save As}
            DisableItem(myMenu, iRevert);               {disable Revert}
            DisableItem(myMenu, iClose);                {disable Close}
        END
    ELSE IF MyGetWindowType(myWindow) = kMyDocWindow THEN
        BEGIN
            myData := MyDocRecHnd(GetWRefCon(myWindow));
            myMenu := GetMHandle(mFile);
            EnableItem(myMenu, iSaveAs);                {enable Save As}
            EnableItem(myMenu, iClose);                 {enable Close}

            IF myData^^.windowDirty THEN
                BEGIN
                    EnableItem(myMenu, iSave);                  {enable Save}
                    EnableItem(myMenu, iRevert);                {enable Revert}
                END
            ELSE
                BEGIN
                    DisableItem(myMenu, iSave);                 {disable Save}
                    DisableItem(myMenu, iRevert);               {disable Revert}
                END;
        END;
END;

Your application should call DoAdjustFileMenu whenever it receives a mouse-down event in the menu bar. (No doubt you want to include code appropriate for enabling and disabling other menu items too.) See the chapter "Menu Manager" in Inside Macintosh: Macintosh Toolbox Essentials for details on the menu enabling and disabling procedures used in Listing 1-19 .


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next